clear all
cd "C:\Users\ot3\Documents\TeacherDeseg\Census"

set more off

clear
quietly infix                ///
  int     year      1-4      ///
  byte    datanum   5-6      ///
  double  serial    7-14     ///
  double  hhwt      15-24    ///
  byte    region    25-26    ///
  byte    statefip  27-28    ///
  byte    urban     29-29    ///
  byte    gq        30-30    ///
  int     pernum    31-34    ///
  double  perwt     35-44    ///
  byte    sex       45-45    ///
  int     age       46-48    ///
  int     birthyr   49-52    ///
  byte    race      53-53    ///
  int     raced     54-56    ///
  byte    hispan    57-57    ///
  int     hispand   58-60    ///
  int     bpl       61-63    ///
  long    bpld      64-68    ///
  byte    educ      69-70    ///
  int     educd     71-73    ///
  byte    empstat   74-74    ///
  byte    empstatd  75-76    ///
  byte    labforce  77-77    ///
  int     occ1950   78-80    ///
  int     ind1950   81-83    ///
  long    inctot    84-90    ///
  long    ftotinc   91-97    ///
  long    incwage   98-103   ///
  int     poverty   104-106  ///
  int     migplac5  107-109  ///
  byte    work5yr   110-110  ///
  byte    milit5yr  111-111  ///
  byte    coll5yr   112-112  ///
  int     occ5yr95  113-115  ///
  int     ind5yr    116-118  ///
  using `"usa_00063.dat"'

replace hhwt     = hhwt     / 100
replace perwt    = perwt    / 100

format serial   %8.0f
format hhwt     %10.2f
format perwt    %10.2f


*Note: Data already restricted to non-Hispanic, black or white

*Race, sex
recode race (1=0)(2=1)(else=.), gen(black)
recode sex (1=1)(2=0), gen(male)


*Current residence regions
g south=inlist(statefip, 1, 5, 12, 13, 22, 28, 37, 45, 47, 48, 51)  
replace south=. if statefip==.

*Residence 5 years ago
drop if migplac5==99 | migplac5==900 //residence 5 yrs ago notspecified/confidential or abroad
g south5=inlist(migplac5, 1, 5, 12, 13, 22, 28, 37, 45, 47, 48, 51)

*Tag cross-region migrants
g migrate=south5!=south



*Current employment category
drop if occ1950==999 

recode empstat (1=0)(2 3=1), gen(outlforce)

recode occ1950 (93=1)(else=0), gen(teacher)
replace teacher=0 if outlforce==1 //respondents report an occupation if they have wroked in past 10 years, even if not working now

recode occ1950 (0/92 94/99=1)(else=0), gen(prof)  
replace prof=0 if outlforce==1 

g nonprof=teacher==0 & prof==0 & outlforce==0

g migrant_teacher=migrate==1 & teacher==1

g nonmigrant_teacher=migrate==0 & teacher==1 

*Employment category 5 years ago
drop if work5yr==9  //"not reported"
recode occ5yr95 (93=1)(else=0), gen(teacher5)
recode occ5yr95 (0/92 94/99=1)(else=0), gen(prof5)

*Earnings
replace incwage=. if incwage>=999998
g learn=ln(incwage)

*Define race-by-region groups
g blackXsouth5=black*south5


save OccFiveYrs, replace





*Table 3: Employment category transitions
use OccFiveYrs, clear 
keep if teacher5==1 & teacher==0 | (teacher5==1 & teacher==1 & migrate==1)

g migteach=teacher5==1 & teacher==1 & migrant==1
g teach_prof=teacher5==1 & prof==1
g teach_nonprof=teacher5==1 & nonprof==1
g teach_outlf=teacher5==1 & outlforce==1


foreach x of varlist migteach teach_nonprof teach_prof teach_outlf {
qui reg `x' black south5 blackXsouth5 i.statefip i.educ  male  age , cluster(statefip)
est sto `x'

}
outreg2 [*] using T1,  keep(black south5 blackXsouth5)  dec(3)   nor2  replace 





*Table 4: Earnings
use OccFiveYrs, clear 
g left_teaching=(teacher5==1 & teacher==0)
g left_teachingXblack=left_teaching*black
g left_teachingXsouth5=left_teaching*south5
g left_teachingXblackXsouth5=left_teaching*black*south5


*southern blacks who left teaching took bigger pay loss than other groups who left teaching
qui reg learn left_teaching i.statefip i.educ  male  age if black==1 & south5==1 & teacher5==1, cluster(statefip)
est sto base
qui reg learn left_teaching black south5 blackXsouth5 left_teachingXblack left_teachingXsouth5 left_teachingXblackXsouth5 i.statefip i.educ  male  age if teacher5==1, cluster(statefip) 
est sto tripdiff

outreg2 [*] using T1,  keep(left_teaching left_teachingXblackXsouth5)  dec(2)   nor2  replace 



*Appendix Table A8: Zeros included
use OccFiveYrs, clear 
g left_teaching=(teacher5==1 & teacher==0)
g left_teachingXblack=left_teaching*black
g left_teachingXsouth5=left_teaching*south5
g left_teachingXblackXsouth5=left_teaching*black*south5

g learn_transf=ln(incwage+1)

qui reg learn_transf left_teaching i.statefip i.educ  male  age if black==1 & south5==1 & teacher5==1, cluster(statefip)
est sto base_zeros
qui reg learn_transf left_teaching black south5 blackXsouth5 left_teachingXblack left_teachingXsouth5 left_teachingXblackXsouth5 i.statefip i.educ  male  age if teacher5==1, cluster(statefip) 
est sto tripdiff_zeros

outreg2 [*] using T1,  keep(left_teaching left_teachingXblackXsouth5)  dec(2)  nocons  nor2  replace 


*Appendix Table A9: No Controls
use OccFiveYrs, clear 
g left_teaching=(teacher5==1 & teacher==0)
g left_teachingXblack=left_teaching*black
g left_teachingXsouth5=left_teaching*south5
g left_teachingXblackXsouth5=left_teaching*black*south5

qui reg learn left_teaching if black==1 & south5==1 & teacher5==1, cluster(statefip)
est sto base_noconts
qui reg learn left_teaching black south5 blackXsouth5 left_teachingXblack left_teachingXsouth5 left_teachingXblackXsouth5  if teacher5==1, cluster(statefip) 
est sto tripdiff_noconts

outreg2 [*] using T1,  keep(left_teaching left_teachingXblackXsouth5)  dec(2)  nocons nor2  replace 



*Appendix Figure A6: Earnings distributions
use OccFiveYrs, clear 
twoway ///
kdensity learn if black==1 & (south5==1 & teacher5==1)  & (south==1 & teacher==1), lpatter(solid) lcolor(black)  || ///
kdensity learn if black==1 & (south5==1 & teacher5==1)  & (teacher==0),   lpattern(dash) lcolor(black) ///
legend(label(1 "Maintained Southern Teaching Position")  label(2 "Exited Southern Teaching Position")  rows(2) size(small)) ///
xtick(4(1)11) xlabel(4(1)11) ///
xtitle(Log Earnings, size(small)) ytitle(Density, size(small)) ///
graphregion(ifcolor(white) fcolor(white) lcolor(white) icolor(white)) 



qui sum learn if black==1 & south5==1 & teacher5==1  & teacher==1
local stayer_mean=`r(mean)'
qui sum learn if black==1 & south5==1 & teacher5==1  & teacher==0
local leaver_mean=`r(mean)'
twoway kdensity learn if black==1 & south5==1 & teacher5==1  & teacher==1, lpatter(dash) lcolor(black) /*xline(`stayer_mean', lwidth(thin) lpatter(dash) lcolor(black))*/   || ///
kdensity learn if black==1 & south5==1 & teacher5==1 & teacher==0,   lpattern(solid) lcolor(black) /*xline(`leaver_mean', lwidth(thin) lpattern(solid) lcolor(black)) */








